package com.example.function.project.model.pojo;


import com.example.common.support.ApplicationContextProvider;
import com.example.function.project.model.one.Project;
import com.example.function.project.model.one.ProjectUserRole;
import com.example.function.project.service.IProjectUserRoleService;
import com.example.function.sys.model.one.SysUser;
import com.example.function.sys.model.pojo.SysUserMiniPojo;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Description: <ProjectUserRolePojo vo>. <br>
 * <p>
 * generate time:2021-7-27 18:02:19
 *
 * @author hanqf
 * @version V1.0
 */
@Data
public class ProjectUserRoleGroupByProjectPojo implements Serializable {
    private static final long serialVersionUID = 1L;


    /*
     * 项目id
     */
    private Long id;

    /*
     * 项目名称
     */
    private String projectName;


    /*
     * 用户id
     */
    private List<SysUserMiniPojo> users;



    /**
     * 实体对象复制到数据对象
     */
    public static ProjectUserRoleGroupByProjectPojo wrapper(Project project) {
        ProjectUserRoleGroupByProjectPojo projectUserRoleGroupByProjectPojo = new ProjectUserRoleGroupByProjectPojo();
        BeanUtils.copyProperties(project, projectUserRoleGroupByProjectPojo);
        final IProjectUserRoleService projectUserRoleService = (IProjectUserRoleService) ApplicationContextProvider.getBean("projectUserRoleService");

        final List<ProjectUserRole> roles = projectUserRoleService.findProjectUserRolesByProjectId(project.getId());
        List<ProjectUserRolePojo> wrapper = ProjectUserRolePojo.wrapper(roles);
        final List<SysUser> sysUsers = wrapper.stream().map(pojo -> pojo.getSysUser()).collect(Collectors.toList());
        projectUserRoleGroupByProjectPojo.setUsers(SysUserMiniPojo.wrapper(sysUsers));

        return projectUserRoleGroupByProjectPojo;
    }

    public static List<ProjectUserRoleGroupByProjectPojo> wrapper(List<Project> projects) {
        List<ProjectUserRoleGroupByProjectPojo> list = new ArrayList<>();
        for (Project project : projects) {
            list.add(ProjectUserRoleGroupByProjectPojo.wrapper(project));
        }
        return list;
    }


}



